Imports System.Windows.Forms

Public Class dlgDepartment

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Try
            If dgvDepartment.Rows.Count > 0 Then
                db.updateTable(dgvDepartment.DataSource, "select departmentid,dep_name from department")
                MsgBox("Saved!!!", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        
    End Sub

    Private Sub txtID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtID.TextChanged
        If db.getvalue("select * from department where departmentid = '" & txtID.Text.Replace("'", "") & "'").Tables(0).Rows.Count > 0 Then
            btnNew.Text = "Update"
            txtName.Text = db.getvalue("select * from department where departmentid='" & txtID.Text.Replace("'", "") & "'", "Dep_name")

        Else
            btnNew.Text = "New"
            txtName.Text = ""
        End If
    End Sub

    Private Sub dlgDepartment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvDepartment.DataSource = db.getvalue("select * from department").Tables(0)
        dgvDepartment.Columns(1).Width = 180
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim sql As String = ""
        If txtID.Text.Length <= 0 Then
            MsgBox("ID is blank!!!")
            Exit Sub
        End If
        If txtName.Text.Length <= 0 Then
            MsgBox("Name is blank!!!")
            Exit Sub
        End If
        If btnNew.Text.ToLower = "new" Then
            sql = "insert department(departmentid,dep_name) values('" & txtID.Text.Replace("'", "") & "','" & txtName.Text.Replace("'", "") & "')"

        End If
        If btnNew.Text.ToLower = "update" Then
            sql = "update department set dep_name='" & txtName.Text.Replace("'", "") & "' where departmentid='" & txtID.Text.Replace("'", "") & "'"
        End If
        Try
            db.update(sql)
            dgvDepartment.DataSource = db.getvalue("select * from department").Tables(0)
            dgvDepartment.Columns(1).Width = 180
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class
